import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:leader_doctor_flutter/common/utils/image_utils.dart';
import 'package:leader_doctor_flutter/common/utils/string_utils.dart';
import 'package:leader_doctor_flutter/styles/colors.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

/// @Author lixiaobin
/// @Date 2023/11/8
/// @Description
class CommWidget {
  /// 通用appbar 带返回按钮
  static AppBar getCommAppbar(BuildContext context, {String? title, List<Widget>? actions}) {
    return AppBar(
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Color(0xFF333333)),
          onPressed: () async {
            pop(context);
          },
        ),
        title: Container(
            child: Text(
          title ?? ModalRoute.of(context)?.settings.name ?? '',
          style: TextStyle(color: const Color(0xFF333333), fontSize: 18.sp),
        )),
        actions: actions,
        backgroundColor: Colors.white,
        shadowColor: ColorRes.colorF2F2F2,
        elevation: 0.3,
        systemOverlayStyle: SystemUiOverlayStyle.dark);
  }

  static void pop(BuildContext context) {
    FocusScope.of(context).unfocus();
    var canPop = context.canPop();
    if (canPop) {
      context.pop();
    } else {
      SystemNavigator.pop();
    }
  }

  /// 通用紫色按钮
  static Widget buildRightBtn(String text, Function onTap, {bool? isSelect = true}) {
    return GestureDetector(
      onTap: () => onTap(),
      child: Container(
        height: 40.w,
        width: double.infinity,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: isSelect == true ? ColorRes.colorMain : ColorRes.colorededfd,
            borderRadius: BorderRadius.circular(6.w)),
        child: Text(text, style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500, fontSize: 16.sp)),
      ),
    );
  }

  /// 通用紫色按钮
  static Widget buildLeftBtn(String text, Function onTap) {
    return GestureDetector(
      onTap: () => onTap(),
      child: Container(
        height: 40.w,
        width: double.infinity,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: ColorRes.colorededfd,
            border: Border.all(color: ColorRes.colorMain, width: 1),
            borderRadius: BorderRadius.circular(6.w)),
        child: Text(text, style: TextStyle(color: ColorRes.colorMain, fontSize: 16.sp)),
      ),
    );
  }

  /// 义诊按钮
  static Widget buildConsultTypeBtn(text, {bool isShow = true}) {
    return Visibility(
        visible: isShow,
        child: GestureDetector(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 4.w, horizontal: 8.w),
            alignment: Alignment.center,
            decoration: BoxDecoration(color: ColorRes.colore6e6ff, borderRadius: BorderRadius.circular(4.w)),
            child: Text(text, style: TextStyle(color: ColorRes.colorMain, fontSize: 10.sp)),
          ),
        ));
  }

  /// 通用分割线
  static Widget buildLine({double? height, Color? color, EdgeInsetsGeometry? margin}) {
    return Container(
      width: double.infinity,
      margin: margin,
      height: height ?? 0.5.w,
      color: color ?? const Color(0xffe1e1e1),
    );
  }

  /// 自定义按钮
  static Widget customBuildBtn(String text, Function onTap, Color? selectColor, Color? uncheckedColor, double? radius,
      {bool? isSelect = true}) {
    return GestureDetector(
      onTap: () => onTap(),
      child: Container(
        height: 40.w,
        width: double.infinity,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: isSelect == true ? (selectColor ?? ColorRes.colorMain) : (uncheckedColor ?? ColorRes.colorB8B3FA),
          borderRadius: BorderRadius.circular(radius ?? 6),
        ),
        child: Text(text, style: TextStyle(color: Colors.white, fontSize: 16.sp, fontWeight: FontWeight.w500)),
      ),
    );
  }

  /// 通用底部弹窗
  static Future buildBottomSheetDialog(BuildContext context,
      {String? title, bool isPadding = true, required Widget? Function(BuildContext context) buildView}) {
    return showMaterialModalBottomSheet(
      expand: false,
      context: context,
      backgroundColor: Colors.transparent,
      builder: (c) => Container(
          padding: EdgeInsets.only(bottom: 28.w),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(topLeft: Radius.circular(16.w), topRight: Radius.circular(16.w))),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                  alignment: Alignment.center,
                  height: 50.w,
                  child: Text(title ?? '温馨提示',
                      style: const TextStyle(fontWeight: FontWeight.bold, color: ColorRes.color333333))),
              CommWidget.buildLine(),
              SizedBox(
                height: 16.w,
              ),
              Container(
                  padding: isPadding
                      ? EdgeInsets.only(
                          left: 28.w,
                          right: 28.w,
                        )
                      : null,
                  child: buildView(c))
            ],
          )),
    );
  }

  /// 无数据布局
  static Widget buildEmptyView({String? icon, String? tipStr}) {
    return Container(
      color: ColorRes.white,
      alignment: Alignment.center,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ImageLoader.loadAsset(icon ?? 'icon_wallet_empty.webp', 0, size: Size(200.w, 200.w)),
          SizedBox(height: 27.w),
          Text(
            tipStr ?? '暂无任何内容',
            style: TextStyle(fontSize: 16.sp, color: ColorRes.colorb4b2ff),
          )
        ],
      ),
    );
  }

  /// 通用蓝色按钮
  static Widget buildBtn2(Text text, Function onTap, Color selelctBtnColor, Color UncheckedBtnColor,
      {bool? isSelect = true}) {
    return GestureDetector(
      onTap: () => onTap(),
      child: Container(
        height: 40.w,
        width: double.infinity,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: isSelect == true ? selelctBtnColor : UncheckedBtnColor, borderRadius: BorderRadius.circular(6.w)),
        child: text,
      ),
    );
  }
}
